<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
      </template>
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        row-key="id"
        :indent="30"
        :tree-props="{
          children: 'nodes',
          hasChildren: 'hasChildren',
        }"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column
          label="部位编号"
          prop="positionCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="部位名称"
          prop="positionName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备种类"
          prop="assetTypeName"
          min-width="120"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="设备编码"
          prop="assetCode"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="设备名称"
          prop="assetName"
          min-width="120"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="序号"
          prop="sort"
          min-width="60"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否停用"
          prop="enabled"
          min-width="70"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="350" align="center">
          <template #default="scope">
            <el-button
              type="success"
              size="small"
              link
              @click="openDialog(scope.row, 'child')"
            >
              <i-ep-plus />
              添加资产组成子部位
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogDetail(scope.row)"
            >
              <i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <eamassetposition-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :assetKind="props.assetKind"
      :parentId="addDialog.parentId"
      :assetTypeId="addDialog.assetTypeId"
      @reset-table="(id:string) => (id?.length ? getTableList() : getTableList())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "EAMEAMASSETPOSITION",
  inheritAttrs: false,
});

const props = defineProps({
  assetKind: {
    type: Number,
    default: 1,
  },
});
// 请求参数
const requestApi = "/eam/eamassetposition/getTree";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  assetTypeId: "",
  positionCode: "",
  positionName: "",
};

const deleteParam = {
  api: "/eam/eamassetposition/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readVisible: false,
  title: "",
  data: {},
  parentId: "0",
  assetTypeId: "",
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/eam/eamassetposition/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开资产组成部位表单弹窗 */
function openDialog(roleObj?: Object, type?: any) {
  if (roleObj && !type) {
    // 编辑
    addDialog.title = `编辑【${roleObj.positionName}】信息`;
    getInfo(roleObj.id);
    addDialog.parentId = "";
    addDialog.assetTypeId = "";
  } else if (roleObj && type) {
    request({
      url: `/eam/eamassetposition/get/${roleObj.id}`,
      method: "get",
    })
      .then((data) => {
        // 添加子机构
        addDialog.title = `【${roleObj.positionName}】添加资产组成子部位`;
        addDialog.parentId = data.data.id;
        addDialog.assetTypeId = data.data.assetTypeId;
        addDialog.data = {
          assetName: data.data.assetName,
          assetCode: data.data.assetCode,
          assetId: data.data.assetId,
        };
      })
      .catch((res) => {
        console.log(res);
      });
  } else {
    // 新增
    addDialog.data = {};
    addDialog.title = "新增资产组成部位";
    addDialog.parentId = "";
    addDialog.assetTypeId = "";
  }
  addDialog.visible = true;
}

/** 打开资产组成部位表单弹窗 详情 */
function openDialogDetail(roleObj?: any) {
  addDialog.title = "资产组成部位详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  addDialog.readvisible = false;
  if (!value) {
    addDialog.parentId = "";
    addDialog.assetTypeId = "";
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/eam/eamassetposition/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
</script>
